<?php
include "helpers.php";
/**
 * 二位数组，无限级数组的转换
 */

/**
 * @param array $data
 * @param int $lv
 * @param string $st
 * @return array
 *  无限级数组向二位数组转换,展示出层级数，层级区别字符串
 *  $json=[{"name":"河南","child":[{"name":"郑州","child":[{"name":"金水区"},{"name":"中原区"}]},{"name":"新乡","child":[{"name":"朝阳区"},{"name":"商业区"}]}]},{"name":"山东","child":[{"name":"青岛","child":[{"name":"青岛1区"},{"name":"青岛2区"}]},{"name":"济南","child":[{"name":"济南1区"},{"name":"济南2区"}]}]}]
 */
function mulTowArray(array $data,$lv=1,$st=''){
    static $da=[];
    foreach($data as $k=>$v){
        $data[$k]['lv']=$lv;
        $data[$k]['st']=$st;
        $tem=[];
        if(isset($v['child'])){
            $tem=$v['child']?:[];
            unset($data[$k]['child']);
        }
        $da[]=$data[$k];
        if($tem){
            mulTowArray($tem,$lv+1,$st.'&nbsp;&nbsp;&nbsp;&nbsp;|');
        }
    }
    return $da;
}

$data=[
    [
        'name'=>'河南',
        'child'=>[
            [
                'name'=>'郑州',
                'child'=>[
                    ['name'=>'金水区'],
                    ['name'=>'中原区']
                ]
            ],
            [
                'name'=>'新乡',
                'child'=>[
                    ['name'=>'朝阳区'],
                    ['name'=>'商业区']
                ]
            ]
        ]
    ],
    [
        'name'=>'山东',
        'child'=>[
            [
                'name'=>'青岛',
                'child'=>[
                    ['name'=>'青岛1区'],
                    ['name'=>'青岛2区']
                ]
            ],
            [
                'name'=>'济南',
                'child'=>[
                    ['name'=>'济南1区'],
                    ['name'=>'济南2区']
                ]
            ]
        ]
    ]
];

echo json_encode($data,JSON_UNESCAPED_UNICODE);die;
$data=mulTowArray($data);

p($data);die;



/**
 * @param $ar
 * @param array $list
 * @param bool $p 是否返回二维数组
 * @return array
 * 得到所有的子集
 */
function get_son($ar,$list=[],$p=false){
    $arr=[];
    while(true){
        $n=count($list)+1;
        foreach($ar as $k=>$v){
            if(in_array($v['pid'],$list)){
                array_push($list,$v['id']);
                array_push($arr,$v);
                unset($ar[$k]);
            }
        }
        if($n>count($list))break;
    }
    return $p?$arr:$list;
}

/**
 * @param $ar
 * @param array $list
 * @param bool $p  是否返回二维数组
 * @return array
 *  得到所有的父级元素，适用于上下级关系的数据
 */
function get_parent($ar,$list=[],$p=false){
    $arr=[];
    while(true){
        $n=count($list)+1;
        foreach($ar as $k=>$v){
            if(in_array($v['id'],$list)){
                array_unshift($list,$v['pid']);
                array_unshift($arr,$v);
                unset($ar[$k]);
            }
        }
        if($n>count($list))break;
    }
    return $p?$arr:$list;
}

//有上下级关系的二维数组，层级化
function mulArray(array $ar,$id='id',$pid='pid'){
    $da=[];
    foreach($ar as $v){
        $da[$v[$id]]=$v;
    }
    $data=[];
    foreach($da as $k=>$v){
        if(isset($da[$v[$pid]])){
            $da[$v[$pid]]['child'][]=&$da[$k];
        }else{
            $data[]=&$da[$k];
        }
    }
    return $data;
}